package gemini.pollux.ui.client.page.navigation;

import gemini.pollux.ui.client.mvp.event.ContainerRequestEvent;
import gemini.pollux.ui.client.mvp.gin.PolluxGinjector;
import gemini.pollux.ui.client.utils.UIConstants.NavigationItem;
import gemini.pollux.ui.client.utils.UIConstants.NavigationTab;

import java.util.ArrayList;
import java.util.List;

import ch.elca.gwt.mvp.client.place.PlaceRequest;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;


public class NavigationWidget extends Composite {
		
	private StackPanel pnlContainer;
	
	private VerticalPanel pnlUser;

	private VerticalPanel pnlTransaction;

	private VerticalPanel pnlProduct;
	
	private VerticalPanel pnlMarketingPlan;
	
	private VerticalPanel pnlStore;
	
	public NavigationWidget() {
		initCustom();
		initWidget(pnlContainer);
	}
	
	private void initCustom(){
		navItems = new ArrayList<NavigationItem>();
		navItems.add(NavigationItem.LEVEL);
		navItems.add(NavigationItem.PRODUCT);
		navItems.add(NavigationItem.PRODUCT_TYPE);
		
		pnlContainer = new StackPanel();
		pnlContainer.setWidth("100%");
		
		pnlUser = new VerticalPanel();
		pnlTransaction = new VerticalPanel();
		pnlProduct = new VerticalPanel();
		pnlStore = new VerticalPanel();
		pnlMarketingPlan = new VerticalPanel();
		
		pnlContainer.add(pnlMarketingPlan, NavigationTab.MARKETING_PLAN.getText());
		pnlContainer.add(pnlUser, NavigationTab.USER.getText());
		pnlContainer.add(pnlTransaction, NavigationTab.TRANSACTION.getText());
		pnlContainer.add(pnlProduct, NavigationTab.PRODUCT.getText());
		pnlContainer.add(pnlStore, NavigationTab.STORE.getText());
		
		for (NavigationItem navItem : navItems) {
			if(navItem.getTab().equals(NavigationTab.USER)){
				pnlUser.add(createNavHtm(navItem));
			} else if(navItem.getTab().equals(NavigationTab.PRODUCT)){
				pnlProduct.add(createNavHtm(navItem));
			} else if(navItem.getTab().equals(NavigationTab.STORE)){
				pnlStore.add(createNavHtm(navItem));
			} else if(navItem.getTab().equals(NavigationTab.TRANSACTION)){
				pnlTransaction.add(createNavHtm(navItem));
			} else if(navItem.getTab().equals(NavigationTab.MARKETING_PLAN)){
				pnlMarketingPlan.add(createNavHtm(navItem));
			}
		}
	}
	
	private HTML currentNavHtm;
	private List<NavigationItem> navItems;	
	private HTML createNavHtm(final NavigationItem navItem){
		HTML navHtm = new HTML(navItem.getText());
		
		navHtm.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(currentNavHtm != null){
					currentNavHtm.removeStyleName("htmError");
				}
				currentNavHtm = (HTML)event.getSource();
				currentNavHtm.addStyleName("htmError");
				clickNavigation(navItem);
			}
		});
		
		return navHtm;
	}
	
	public void clickNavigation(NavigationItem navItem){
		
		if(navItem.equals(NavigationItem.LEVEL)){
			PolluxGinjector.INSTANCE.getEventBus().fireEvent(new ContainerRequestEvent(
					new PlaceRequest(PolluxGinjector.INSTANCE.getLevelPresenter().getPlace())));
		} else if(navItem.equals(NavigationItem.PRODUCT_TYPE)){
			PolluxGinjector.INSTANCE.getEventBus().fireEvent(new ContainerRequestEvent(
					new PlaceRequest(PolluxGinjector.INSTANCE.getProductTypePresenter().getPlace())));
		} else if(navItem.equals(NavigationItem.PRODUCT)){
			PolluxGinjector.INSTANCE.getEventBus().fireEvent(new ContainerRequestEvent(
					new PlaceRequest(PolluxGinjector.INSTANCE.getProductPresenter().getPlace())));
		}
	}
}
